package com.example.demo.config;

import cn.creditease.std.config.JobConsumerConfig;
import cn.creditease.std.factory.JobAlloterFactory;
import cn.creditease.std.factory.JobAlloterFactoryBuilder;
import cn.creditease.std.utils.IpUtils;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: kai.yang
 * @Date: 2019-08-13 10:11
 */
@Configuration
public class AlloterConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ConnectionFactory connectionFactory;

    @Autowired
    DataSource dataSource;

    @Value("${server.port}")
    private int port;


    @Bean
    public JobAlloterFactory getJobAlloterManager() {
        JobConsumerConfig jobConsumerConfig = new JobConsumerConfig().
                setMaxJobCount(40).setMaxConsumerCountByJobGroupPerServer(3);
        Map<String, JobConsumerConfig> jobConsumerConfigMap = new HashMap<String, JobConsumerConfig>();
        jobConsumerConfigMap.put("test", jobConsumerConfig);

        return new JobAlloterFactoryBuilder().setConnectionFactory(connectionFactory)
            .setConsumerId(IpUtils.getLocalIp() + ":" + port).setDataSource(dataSource).setJobConsumerConfigMap(jobConsumerConfigMap)
            .setRabbitTemplate(rabbitTemplate).buildMqFactory();

    }

}
